package com.wushi.lenovo.asia5b.app_di.goods.presenter;

import android.text.TextUtils;

import com.mvp.base.BasePresenter;
import com.mvp.base.NetParams;
import com.mvp.base.model.ResultModel;
import com.mvp.common.LocalManageUtil;
import com.wushi.lenovo.asia5b.app_config.UserRepository;
import com.wushi.lenovo.asia5b.app_di.goods.mvpview.OrderDetailFragmentView;
import com.wushi.lenovo.asia5b.app_http.ApiService;
import com.wushi.lenovo.asia5b.app_http.OrderService;
import com.wushi.lenovo.asia5b.app_model.good.OrderLogistic;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;

import java.util.Map;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;


/**
 * Created by zhangyuncai on 2019/1/26.
 */
public class OrderDetailFragmentPresenter extends BasePresenter<OrderDetailFragmentView> {

    ApiService apiService;
    OrderService orderService;

    @Inject
    public OrderDetailFragmentPresenter(ApiService apiService, OrderService orderService) {
        this.apiService = apiService;
        this.orderService = orderService;
    }

    public void getOrderDetail(int order_id) {
        baseSubscribe(orderService.getOrderDetail(UserInfo.getMemberId(), LocalManageUtil.getAppLanguage(), order_id),
                orderDetailModelResultModel -> {
                    getView().getOrderDetailResult(orderDetailModelResultModel);
                });

    }

    public void getLogisticStatus(int order_id, Consumer<ResultModel<OrderLogistic>> consumer) {
        baseSubscribe(orderService.getLogisticStatus(order_id, UserInfo.getMemberId()),
                consumer);
    }

    /**
     * 获取国际物流
     * @param data_track
     */
    public void getOrderTrackData(int data_track) {
        baseSubscribe(orderService.getOrderTrackData(UserInfo.getMemberId(), LocalManageUtil.getAppLanguage(), data_track)
                , NetParams.Companion.isToastError(false),
                listResultModel -> {
                    getView().getOrderTrackDataResult(listResultModel);
                });

    }

    /**
     * 获取中国物流
     * @param data_track
     */
    public void getOrderParcelTrackData(int data_track) {
        baseSubscribe(orderService.getOrderParcelTrackData(UserInfo.getMemberId(), LocalManageUtil.getAppLanguage(), data_track)
                , NetParams.Companion.isToastError(false),
                resModelResultModel -> {
                    getView().getOrderParcelTrackDataResult(resModelResultModel);
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().getOrderParcelTrackDataResult(null);
                    }
                });

    }

    /**
     * @param map
     * @param suffixPath 类似orderdg.detail.received
     */
    public void button_order(Map<String, Object> map, String suffixPath) {
        if (!UserRepository.isLogin() || TextUtils.isEmpty(suffixPath)) {
            return;
        }
        baseSubscribe(orderService.button_order(suffixPath, map),
                resultModel -> {
                    if (UserRepository.isLogin()) {//无人登录的情况下仍然显示数据
                        getView().button_orderResult(resultModel, map);
                    }
                });
    }

}